package everydayGrade.t5;

import CommonStructure.TreeNode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * @Author: Siantar
 * @Date: 2023-12-15-20:56
 * @Description: 1.0
 */
public class T2415 {
    public TreeNode reverseOddLevels(TreeNode root) {
        Queue<TreeNode> queue = new ArrayDeque<>();
        boolean b = false;
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<TreeNode> list = new ArrayList<>();
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode t = queue.poll();
                if (t.left != null) {
                    queue.offer(t.left);
                    queue.offer(t.right);
                }
                if (b) {
                    list.add(t);
                }
            }
            if (b) {
                for (int l = 0, r = size - 1; l < r; l++, r--) {
                    int t = list.get(l).val;
                    list.get(l).val = list.get(r).val;
                    list.get(r).val = t;
                }
            }
            b ^= true;
        }
        return root;
    }
}
